package net.chrisrichardson.ec2deployer.core

import org.apache.commons.logging.*
import org.apache.commons.httpclient.HttpClient
import groovy.mock.interceptor.MockFor
import org.apache.commons.httpclient.methods.GetMethod;

public class EC2RequestExecutorTest extends GroovyTestCase {

    def Log logger = LogFactory.getLog(getClass())
    def accessKey = '10QMXFEV71ZS32XQFTR2'
	def request = ['Action' : 'DescribeImages',  'AWSAccessKeyId' : accessKey,  'SignatureVersion' : '1',  'Timestamp' : '2006-12-08T07%3A48%3A03Z',  'Version' : '2007-01-03']
	def secretKey = 'DMADSSfPfdaDjbK+RRUhS/aDrjsiZadgAUm8gRU2'
	EC2RequestExecutor requestor
	
	public void setUp() {
		super.setUp()
		def props = new Properties()
		props['accessKey'] = accessKey
		props['secretKey'] = secretKey
        AWSProperties awsProperties = new AWSProperties(props)
		requestor = new EC2RequestExecutor(awsProperties);
	}
	
	public void testHash() {
		def result = requestor.encryptRequest(request, secretKey)
		logger.debug(result)
		assert 'GjH3941IBe6qsgQu+k7FpCJjpnc=' == result
	
	}

	public void testDataToHash() {
		def result = requestor.makeDataToHash(request)
		assert 'ActionDescribeImagesAWSAccessKeyId10QMXFEV71ZS32XQFTR2SignatureVersion1Timestamp2006-12-08T07:48:03ZVersion2007-01-03' == result
	
	}
	

	public void testRequest() {
		def request = ['Action' : 'DescribeImages',  'Owner.1' : '556565656']

		def mockForHttpClient = new MockFor(HttpClient)
		def mockForGetMethod = new MockFor(GetMethod)

        mockForHttpClient.demand.executeMethod{200}
        mockForGetMethod.demand.getResponseBodyAsStream { new StringReader("""<Response/>""")}
        
		mockForHttpClient.use {
            mockForGetMethod.use {
                requestor.executeRequest(request)
            }
        }
	}

}
